package com.ruoyi.web.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.domain.UserEmpower;
import com.ruoyi.web.service.IUserEmpowerService;
import com.ruoyi.web.vo.UserEmpowerPageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户授权Controller
 *
 * @author ruoyi
 * @date 2023-01-03
 */
@RestController
@RequestMapping("/empower")
public class UserEmpowerController extends BaseController {
    @Autowired
    private IUserEmpowerService userEmpowerService;


    @PostMapping
    public R<String> save(@RequestBody UserEmpower empower) {
        return userEmpowerService.save(empower);
    }

    /**
     * 查询用户授权列表
     */
    @PreAuthorize("@ss.hasPermi('web:empower:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserEmpower userEmpower) {
        startPage();
        List<UserEmpower> list = userEmpowerService.selectUserEmpowerList(userEmpower);
        TableDataInfo info = getDataTable(list);

        List<UserEmpowerPageVO> voList = new ArrayList<>();
        for (UserEmpower user : list) {
            UserEmpowerPageVO vo = new UserEmpowerPageVO();
            BeanUtils.copyProperties(user, vo);
            voList.add(vo);
        }
        return getDataTable(info, voList);
    }

    /**
     * 导出用户授权列表
     */
    @PreAuthorize("@ss.hasPermi('web:empower:export')")
    @Log(title = "用户授权", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserEmpower userEmpower) {
        List<UserEmpower> list = userEmpowerService.selectUserEmpowerList(userEmpower);
        ExcelUtil<UserEmpower> util = new ExcelUtil<UserEmpower>(UserEmpower.class);
        util.exportExcel(response, list, "用户授权数据");
    }

    /**
     * 获取用户授权详细信息
     */
    @PreAuthorize("@ss.hasPermi('web:empower:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(userEmpowerService.selectUserEmpowerById(id));
    }

    /**
     * 修改用户授权
     */
    @PreAuthorize("@ss.hasPermi('web:empower:edit')")
    @Log(title = "用户授权", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserEmpower userEmpower) {
        return toAjax(userEmpowerService.updateUserEmpower(userEmpower));
    }

    /**
     * 删除用户授权
     */
    @PreAuthorize("@ss.hasPermi('web:empower:remove')")
    @Log(title = "用户授权", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(userEmpowerService.deleteUserEmpowerByIds(ids));
    }
}
